#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>


sem_t sem;
int data = 0;

void *fun1(void *arg)
{

	while(1)
	{
		printf("please input data:\n");
		scanf("%d",&data);
		sem_post(&sem);
	}
}

void *fun2(void *arg)
{
	while(1)
	{
		sem_wait(&sem);

		printf("data = %d\n",data);
	}


}

int main()
{

	pthread_t tid1 = 0,tid2 = 0;
	int ret = 0;

	sem_init(&sem,0,0);

	ret = pthread_create(&tid1,NULL,fun1,NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(-1);
	}

	ret = pthread_create(&tid2,NULL,fun2,NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(-1);
	}
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	sem_destroy(&sem);


	return 0;

}





